package zgc.demo.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import zgc.demo.consumer.entity.Goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order3")
public class OrderController03 {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 使用负载均衡器监护rest实现远程接口调用
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/getGoodsById/{goodsId}")
    public Map<String, Goods> getGoodsById(@PathVariable("goodsId") String goodsId) {
        //使用负载均衡器简化rest调用 服务名称必须大写
        String serviceName = "PROVIDER";//使用服务名称作为ip+port

        //4.动态创建url
        //String url = "http://" + host + ":" + port + "/goods/getGoodsById/" + goodsId;
        String url = "http://" + serviceName + "/goods/getGoodsById/" + goodsId;
        //5.远程接口请求

        return restTemplate.getForObject(url, Map.class);

    }

}
